<?php
	include_once('header.php');
	// start 发布/修改帖子到数据库
	if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
		$title_new_article = $_POST['title_new_article'];
		$title_new_article = addslashes($title_new_article);
		$content_new_article = $_POST['content_new_article'];
		$content_new_article = addslashes(nl2br($content_new_article));
		// start 判断是修改帖子或者是全新发布帖子，设置不同的sql语句
		if (@$_POST['isnot_edit_hidden']=='true') {
			$query_new_article = "update `articles` set `title`='".$title_new_article."',`content`='".$content_new_article."',`post_time`=now()";
		} else {
			$query_new_article = "insert into `articles` (`title`, `content`, `post_time`) values ('".$title_new_article."', '".$content_new_article."', now())";
		}
		// end 判断是修改帖子或者是全新发布帖子，设置不同的sql语句
		$mysql_connection->query($query_new_article);
		if (@$_POST['isnot_edit_hidden']=='true') {
			$article_id_post = $_POST['edit_id_hidden'];
			// start 删除要修改帖子的分类信息
			$query_del_edit_category = "delete from `articles_category` where `articles_id`=".$article_id_post;
			$mysql_connection->query($query_del_edit_category);
			// end 删除要修改帖子的分类信息
		} else {
			// start 取得刚刚存入数据库的帖子的id
			$query_article_id = "select `id` from `articles` order by `post_time` desc limit 1";
			$result_article_id = $mysql_connection->query($query_article_id);
			$article_id_new_post = $result_article_id->fetch_assoc();
			$article_id_post = $article_id_new_post['id'];
			// end 取得刚刚存入数据库的帖子的ID
		}
		// start 设置帖子的分类到数据库
		$category_id_new_post = @$_POST['category_new_post'];
		if (isset($category_id_new_post)) {
			foreach ($category_id_new_post as $a => $b) {
				$query_articles_category = "insert into `articles_category` (`articles_id`, `category_id`) values (".$article_id_post.", ".$b.")";
				$mysql_connection->query($query_articles_category);
			}
		}
		// end 设置帖子的分类到数据库
	}
	// end 发布/修改帖子到数据库
	// start 如果是修改帖子，给表单预设值
	if (isset($_GET['edit_id'])) {
		$article_edit_id = $_GET['edit_id'];
		$query_edit_article = "select * from `articles` where `id`=".$article_edit_id;
		$result_edit_article = $mysql_connection->query($query_edit_article);
		$article_edit = $result_edit_article->fetch_assoc();
		//$edit_bool = true;
		// start 取得帖子分类ID的数组
		$query_category_ids = "select `category_id` from `articles_category` where `articles_id`=".$article_edit_id;
		$result_category_ids = $mysql_connection->query($query_category_ids);
		$category_ids = array();
		$count_category_ids = $result_category_ids->num_rows;
		for ($i=0; $i<$count_category_ids; $i++) {
			$each_category_ids = $result_category_ids->fetch_assoc();
			$category_ids[] += $each_category_ids['category_id'];
		}
		// end 取得帖子分类ID的数组
	}
	// end 如果是修改帖子，给表单预设值
?> 
<!-- start javascript函数 -->
<script language="javascript">
	function showUpload() {
		var upload_form = document.getElementById("form_upload_file");
		upload_form.style.display = "block";
	}
</script>
<!-- end javascript函数 -->
<!-- start 发布帖子表单 -->
<div id="content_b">
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>?message_box=文章已经发布！" method="post">
		<div id="title_and_content">
		标题：<input type="text" name="title_new_article" id="title_new_article" value="<?php if(isset($_GET['edit_id'])){echo $article_edit['title'];}?>" /><br />
		内容：<textarea name="content_new_article" id="content_new_article" rows="<?php echo $setting['textarea_height'];?>"><?php
			// start 判断是否有上传过的文件
			if ( isset($_GET['file_id']) ) {
				$file_id = $_GET['file_id'];
				$query_file = "select * from `files` where `id`=".$file_id;
				$result_file = $mysql_connection->query($query_file);
				$result_file = $result_file->fetch_assoc();
			?><a href="<?php echo $result_file['url']; ?>"><?php echo $result_file['name']; ?></a><?php
			}
			// end 判断是否有上传过的文件
			// start 如果是修改帖子，显示帖子内容
			if (isset($_GET['edit_id'])) {
				$article_edit_content = str_replace("<br />","",$article_edit['content']);
				echo $article_edit_content;
			}
			// end 如果是修改帖子，显示帖子内容
		?></textarea>
		</div>
		<div id="iframe_new_post">
		<p><a href="javascript:showUpload();">上传文件</a></p>
		<!-- start 上传文件表单 -->
		<iframe name="form_upload_file" id="form_upload_file" height="80" width="400" frameBorder="0" src="upload_file.php"></iframe>
		</div>
		<!-- 上传文件表单 END -->
		<div id="category_new_post">
			<p>选择分类：</p>
			<?php
				// start 列出所有分类
				$category_new_post = select_category($mysql_connection);
				foreach ($category_new_post as $category_id => $category_name) {
					if ($setting['default_category_id']==$category_id) {
					?>
					<input checked type="checkbox" <?php if(@in_array($category_id,$category_ids)){echo 'checked';}?> name="category_new_post[]" id="<?php echo $category_id.'category_id_chechbox';?>" value ="<?php echo $category_id; ?>" />
					<label for="<?php echo $category_id.'category_id_chechbox';?>" ><?php echo $category_name; ?><label><br />
					<?php
					} else {
					?> 
					<input type="checkbox" <?php if(@in_array($category_id,$category_ids)){echo 'checked';}?> name="category_new_post[]" id="<?php echo $category_id.'category_id_chechbox';?>" value ="<?php echo $category_id; ?>" />
					<label for="<?php echo $category_id.'category_id_chechbox';?>" ><?php echo $category_name; ?><label><br />
					<?php
					}
				}
				// end 列出所有分类
				if (isset($_GET['edit_id'])) {
				?>
				<input type="hidden" name="isnot_edit_hidden" id="isnot_edit_hidden" value="true" />
				<input type="hidden" name="edit_id_hidden" id="edit_id_hidden" value="<?php echo $_GET['edit_id'];?>" />
				<?php
				}
			?>
		</div>
		<div id="submit_new_post"><input type="submit" value="发 布" /></div>
	</form>
</div>
<!-- end 发布帖子表单 -->
<?php include_once('footer.php'); ?> 
